package com.restaurant.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.restaurant.domain.Foodtype;
import com.restaurant.service.FoodtypeService;
import com.restaurant.validators.FoodTypeFormDeleteValidator;
import com.restaurant.validators.FoodTypeFormValidator;

@Component
@RequestMapping("/action/foodtype.do")
@SessionAttributes("foodtype")
public class FoodTypeFormController {

	private final String query = "from Foodtype where isdeleted = false";
	
	private FoodtypeService objFoodtypeService;
	
	private FoodTypeFormValidator foodTypeFormValidator;
	
	@Autowired
	private FoodTypeFormDeleteValidator foodTypeFormDeleteValidator;
	
	
	
	public FoodtypeService getObjFoodtypeService() {
		return objFoodtypeService;
	}

	@Autowired
	public void setObjFoodtypeService(FoodtypeService objFoodtypeService) {
		this.objFoodtypeService = objFoodtypeService;
	}

	public FoodTypeFormValidator getFoodTypeFormValidator() {
		return foodTypeFormValidator;
	}

	@Autowired
	public void setFoodTypeFormValidator(FoodTypeFormValidator foodTypeFormValidator) {
		this.foodTypeFormValidator = foodTypeFormValidator;
	}

	@RequestMapping(method = RequestMethod.GET)
	public String foodTypeGETRequest(ModelMap map){
		Foodtype foodtype = new Foodtype();
		ArrayList<Foodtype> listFoodType = null;
		
		try {
			listFoodType = (ArrayList<Foodtype>) objFoodtypeService.loadByHql(query);
		} catch (Exception e) {
			listFoodType = new ArrayList<Foodtype>();
		}
		
		map.addAttribute("foodtype", foodtype);
		map.addAttribute("listFoodType", listFoodType);
		return "FoodType";
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String foodTypePOSTRequest(ModelMap map, @ModelAttribute ArrayList<Foodtype> listFoodType, Foodtype foodtype, BindingResult result, HttpServletRequest request, HttpServletResponse response){
		
		String action = request.getParameter("action");
		
		try{
			if(action.equals("addnew")){
				foodTypeFormValidator.validate(foodtype, result);
				if(result.hasErrors()){
					try {
						listFoodType = (ArrayList<Foodtype>) objFoodtypeService.loadByHql(query);
					} catch (Exception e) {
						e.printStackTrace();
					}
					map.addAttribute("listFoodType", listFoodType);
					return "FoodType";
				}
				foodtype.setIsdeleted(false);
				objFoodtypeService.addOrModify(foodtype);
				foodtype = new Foodtype();
			}else if(action.equals("edit")){
				foodtype = objFoodtypeService.loadByPrimaryKey(Long.parseLong(request.getParameter("id")));
				objFoodtypeService.addOrModify(foodtype);
			}else if(action.equals("del")){
				foodtype = objFoodtypeService.loadByPrimaryKey(Long.parseLong(request.getParameter("id")));
				foodTypeFormDeleteValidator.validate(foodtype, result);
				if(result.hasErrors()){
					try {
						listFoodType = (ArrayList<Foodtype>) objFoodtypeService.loadByHql(query);
					} catch (Exception e) {
						e.printStackTrace();
					}
					map.addAttribute("listFoodType", listFoodType);
					return "FoodType";
				}
				foodtype.setIsdeleted(true);
				objFoodtypeService.addOrModify(foodtype);
				foodtype = new Foodtype();
			}else{
				return "redirect:foodtype.do";
			}
			
		}catch(Exception e){
			return "redirect:foodtype.do";
		}
		
		try {
			listFoodType = (ArrayList<Foodtype>) objFoodtypeService.loadByHql(query);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		map.addAttribute("foodtype", foodtype);
		map.addAttribute("listFoodType", listFoodType);
		return "FoodType";
	}
}
